﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Inovout.Business;
using System.Collections.Specialized;
using System.Collections;
using Inovout.Gui.Wpf.Views;
using System.Linq;

namespace Inovout.Gui.Wpf.ViewModels
{
    public abstract class ModelCollectionViewModel<TModel> : ModelCollectionViewModel, INotifyCollectionChanged
        , IList<ModelViewModel<TModel>>, ICollection<ModelViewModel<TModel>>, IEnumerable<ModelViewModel<TModel>>
       // , IList, ICollection, IEnumerable 
        where TModel : class
    {
        protected override Type ModelType
        {
            get { return typeof(TModel); }
        }
        public event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add { items.CollectionChanged += value; }
            remove { items.CollectionChanged -= value; }
        }

        private ObservableCollection<ModelViewModel<TModel>> items;
        public ObservableCollection<ModelViewModel<TModel>> Items
        {
            get
            {
                items.First();
                return items;
            }
        }

        public override ViewContext ViewContext
        {
            get
            {
                return base.ViewContext;
            }
        }

        public override void Init(ViewContext viewContext)
        {
            base.Init(viewContext);
            items = new ObservableCollection<ModelViewModel<TModel>>();
            IEnumerable<TModel> list = ViewContext.WorkItemContext.ViewData.Model as IEnumerable<TModel>;
            foreach (TModel model in list)
            {
                Type modelType = typeof(TModel);

                ModelViewModel<TModel> modelViewModel = ComponentRegistry.Resolve<ModelViewModel<TModel>>(modelType.Name.ToLower() + "ViewModel".ToLower());
                modelViewModel.ViewData = ComponentRegistry.Resolve<IViewDataDictionary<TModel>>();
                modelViewModel.ViewData.Model = model;
                items.Add(modelViewModel);
            }
        }
        public int IndexOf(ModelViewModel<TModel> item)
        {
            return items.IndexOf(item);
        }

        public void Insert(int index, ModelViewModel<TModel> item)
        {
            items.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            items.RemoveAt(index);
        }

        public ModelViewModel<TModel> this[int index]
        {
            get
            {
                return items[index];
            }
            set
            {
                items[index] = value;
            }
        }

        public void Add(ModelViewModel<TModel> item)
        {
            items.Add(item);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(ModelViewModel<TModel> item)
        {
            return items.Contains(item);
        }

        public void CopyTo(ModelViewModel<TModel>[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return items.Count; }
        }

        public bool IsReadOnly
        {
            get { return ((ICollection<ModelViewModel<TModel>>)items).IsReadOnly; }
        }

        public bool Remove(ModelViewModel<TModel> item)
        {
            return items.Remove(item);
        }

        public IEnumerator<ModelViewModel<TModel>> GetEnumerator()
        {
            return items.GetEnumerator();
        }



        IEnumerator IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }
}
